//
//  LKMySubordinateSearchResultViewController.m
//  Mideleague_Agent
//
//  Created by Zhiwang on 2020/6/17.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKMySubordinateSearchResultViewController.h"
#import "LKMyShopListCell.h"
#import "LKMySubordinateDetailViewController.h"
#import "LKMySuboraListModel.h"
#import "LKMySubordinateSearchResultViewController.h"
@interface LKMySubordinateSearchResultViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchControllerDelegate>

@property(nonatomic,strong)LKBaseTableview *tableView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,copy)NSString * timeType;
@property(nonatomic,copy)NSString * priceType;
@property(nonatomic,copy)NSString * levelType;
@property(nonatomic,strong)NSMutableArray *levelArray;
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,strong)NSMutableDictionary *paramDic;
@property(nonatomic,copy)NSString *currentSelectedKey;
@property(nonatomic,copy)NSString *currentSelectedValue;
@property(nonatomic,copy)NSString *currentStr;

@end

@implementation LKMySubordinateSearchResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.backButton.hidden = YES;
    self.priceType = @"1";
    self.timeType = @"1";
    self.levelType = @"1";
    self.currentSelectedKey = @"sort_type";
    self.currentSelectedValue = self.priceType;
    [self createMytabelView];
    
    // Do any additional setup after loading the view.
}

/// 创建tableview列表
-(void)createMytabelView{
    self.tableView = [LKBaseTableview setTableviewWithFrame:CGRectZero style:UITableViewStyleGrouped registercellClasss:@[@"LKMyShopListCell"]];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.emptyDataSetSource = self;
    _tableView.emptyDataSetDelegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:_tableView];
    if (@available(iOS 13.0, *)) {
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.view).offset(-10);
            make.bottom.equalTo(self.view).offset(0);
        }];
    } else {
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.view).offset(-10);
            make.bottom.equalTo(self.view).offset(0);
        }];
    }
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerReloadData)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerReloadData)];
}
/// 上拉刷新
-(void)headerReloadData{
    self.page = 0;
    [self.paramDic setObject:@(self.page) forKey:@"page"];
    [self getMySubordinateListDataWithType:self.currentStr  AndWithKey:@"word"];
}

/// 下拉加载更多
-(void)footerReloadData{
    self.page++;
    [self.paramDic setObject:@(self.page) forKey:@"page"];
    [self getMySubordinateListDataWithType:self.currentStr  AndWithKey:@"word"];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LKMyShopListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKMyShopListCell"];
    cell.listType = CellListTypeMySuboradinate;
    cell.mySuboralistModel = self.dataArray[indexPath.section];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 135.0f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] init];
    return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.000000001f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] init];
    return footerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1.0f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LKMySuboraListModel *model = self.dataArray[indexPath.section];
    LKMySubordinateDetailViewController *vc = [[LKMySubordinateDetailViewController alloc] init];
    vc.uid = model.uid;
    [self.presentingViewController.navigationController pushViewController:vc animated:YES];
}
//获取我的下级代理列表数据
-(void)getMySubordinateListDataWithType:(NSString *)type AndWithKey:(NSString *)key{
    //@"time_type":self.timeType
    self.paramDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:type,key,@(YES),@"is_search",self.priceType,@"sort_type", nil];
//    if (self.uid) {
//        if ([key isEqualToString:@"word"]) {
//            param = [[NSMutableDictionary alloc] initWithObjectsAndKeys:self.uid,@"id",type,key,@(YES),@"is_search", nil];
//        }else{
//            param = [[NSMutableDictionary alloc] initWithObjectsAndKeys:self.uid,@"id",type,key,@(self.page),@"page", nil];
//        }
//
//    }else{
//        if ([key isEqualToString:@"word"]) {
//            param = [[NSMutableDictionary alloc] initWithObjectsAndKeys:type,key,@(YES),@"is_search", nil];
//        }else{
//            param = [[NSMutableDictionary alloc] initWithObjectsAndKeys:type,key,@(self.page),@"page", nil];
//        }
//
//    }
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MySuboradinateListUrl header:[LKUserInforTool account].token parameters:self.paramDic progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        if ([responseObject[@"code"] isEqualToString:@"success"]) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            if (self.page == 0) {
                [self.dataArray removeAllObjects];
            }
            NSArray *dataArray = responseObject[@"data"];
            if (dataArray.count < 15) {
                for (NSDictionary *dic in dataArray) {
                    LKMySuboraListModel *model = [LKMySuboraListModel modelWithDictionary:dic];
                    [self.dataArray addObject:model];
                }
                self.tableView.mj_footer.hidden = YES;
            }else{
                for (NSDictionary *dic in dataArray) {
                    LKMySuboraListModel *model = [LKMySuboraListModel modelWithDictionary:dic];
                    [self.dataArray addObject:model];
                }
            }
            [self.tableView reloadData];
        }else{
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
        }
        
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
//谓词搜索过滤
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    
        //修改"Cancle"退出字眼,这样修改,按钮一开始就直接出现,而不是搜索的时候再出现
//        searchController.searchBar.showsCancelButton = YES;
//    [searchController.searchBar becomeFirstResponder];
    
//    searchController.searchBar.showsCancelButton = YES;
//        for(id sousuo in [[searchController.searchBar subviews][0] subviews])
//        {
//
//            for (id zz in [sousuo subviews])
//            {
//
//                if([zz isKindOfClass:[UIButton class]]){
//                    UIButton *btn = (UIButton *)zz;
//                    [btn setTitle:@"取消" forState:UIControlStateNormal];
//                    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
////                    [btn addTarget:self action:@selector(gobackMainpage) forControlEvents:UIControlEventTouchUpInside];
//                }
//
//
//            }
//        }
//
    self.currentStr = searchController.searchBar.text;
    NSLog(@"updateSearchResultsForSearchController");
    [self getMySubordinateListDataWithType:searchController.searchBar.text AndWithKey:@"word"];
}



#pragma mark - UISearchControllerDelegate代理
//测试UISearchController的执行过程

- (void)willPresentSearchController:(UISearchController *)searchController
{
    NSLog(@"willPresentSearchController");
}

- (void)didPresentSearchController:(UISearchController *)searchController
{
    NSLog(@"didPresentSearchController");

//    [self.view addSubview:self.searchController.searchBar];
}

- (void)willDismissSearchController:(UISearchController *)searchController
{
    NSLog(@"willDismissSearchController");
}

- (void)didDismissSearchController:(UISearchController *)searchController
{
    NSLog(@"didDismissSearchController");
}

- (void)presentSearchController:(UISearchController *)searchController
{
    NSLog(@"presentSearchController");
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
